home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 251_01 / advint.c < prev    next >
Text File  |  1987-10-27  |  3KB  |  133 lines

  1. /* advint.c - an interpreter for adventure games */
  2. /*
  3.     Copyright (c) 1986, by David Michael Betz
  4.     All rights reserved
  5. */
  6.  
  7. #include "advint.h"
  8. #include "advdbs.h"
  9. #ifndef MAC
  10. #include <setjmp.h>
  11. #endif
  12.  
  13. /* global variables */
  14. jmp_buf restart;
  15.  
  16. /* external variables */
  17. extern int h_init;
  18. extern int h_update;
  19. extern int h_before;
  20. extern int h_after;
  21. extern int h_error;
  22.  
  23. /* main - the main routine */
  24. main(argc,argv)
  25.   int argc; char *argv[];
  26. {
  27.     char *fname,*lname;
  28.     int rows,cols,i;
  29.  
  30. #ifdef MAC
  31.     char name[50];
  32.     macinit(name);
  33.     fname = name;
  34.     lname = NULL;
  35.     rows = 20;
  36.     cols = 80;
  37. #else
  38.     printf("ADVINT v1.2 - Copyright (c) 1986, by David Betz\n");
  39.     fname = NULL;
  40.     lname = NULL;
  41.     rows = 24;
  42.     cols = 80;
  43.  
  44.     /* parse the command line */
  45.     for (i = 1; i < argc; i++)
  46.     if (argv[i][0] == '-')
  47.         switch (argv[i][1]) {
  48.         case 'r':
  49.         case 'R':
  50.             rows = atoi(&argv[i][2]);
  51.             break;
  52.         case 'c':
  53.         case 'C':
  54.             cols = atoi(&argv[i][2]);
  55.             break;
  56.         case 'l':
  57.         case 'L':
  58.             lname = &argv[i][2];
  59.                 break;
  60.         }
  61.     else
  62.         fname = argv[i];
  63.     if (fname == NULL) {
  64.     printf("usage: advint [-r<rows>] [-c<columns>] [-l<log-file>] <file>\n");
  65.     exit();
  66.     }
  67. #endif
  68.  
  69.     /* initialize terminal i/o */
  70.     trm_init(rows,cols,lname);
  71.  
  72.     /* initialize the database */
  73.     db_init(fname);
  74.  
  75.     /* play the game */
  76.     play();
  77. }
  78.  
  79. /* play - the main loop */
  80. play()
  81. {
  82.     /* establish the restart point */
  83.     setjmp(restart);
  84.  
  85.     /* execute the initialization code */
  86.     execute(h_init);
  87.  
  88.     /* turn handling loop */
  89.     for (;;) {
  90.  
  91.     /* execute the update code */
  92.     execute(h_update);
  93.  
  94.     /* parse the next input command */
  95.     if (parse()) {
  96.         if (single())
  97.         while (next() && single())
  98.             ;
  99.     }
  100.  
  101.     /* parse error, call the error handling code */
  102.     else
  103.         execute(h_error);
  104.     }
  105. }
  106.  
  107. /* single - handle a single action */
  108. int single()
  109. {
  110.     /* execute the before code */
  111.     switch (execute(h_before)) {
  112.     case ABORT:    /* before handler aborted sequence */
  113.     return (FALSE);
  114.     case CHAIN:    /* execute the action handler */
  115.     if (execute(getafield(getvalue(V_ACTION),A_CODE)) == ABORT)
  116.         return (FALSE);
  117.     case FINISH:/* execute the after code */
  118.     if (execute(h_after) == ABORT)
  119.         return (FALSE);
  120.     break;
  121.     }
  122.     return (TRUE);
  123. }
  124.  
  125. /* error - print an error message and exit */
  126. error(msg)
  127.   char *msg;
  128. {
  129.     trm_str(msg);
  130.     trm_chr('\n');
  131.     exit();
  132. }
  133.